<template>
    <div>
        <el-dialog title="详情" v-model="isShowDialog" width="800px" @close="closeDialog">
            <el-form :model="formModel" :inline="true" :disabled="true" size="default" label-width="120px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="任务名称" prop="taskName">
                            <el-input v-model="formModel.taskName" show-word-limit maxlength="50" placeholder=""
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="任务类型" prop="taskType">
                            <el-select v-model="formModel.taskType" placeholder="">
                                <el-option v-for="item in DictInfo.taskType" :key="item.key" :label="item.label"
                                    :value="item.key" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="源库" prop="sourceDatabase">
                            <DatabaseSelect :default-value="formModel.sourceDatabase" :disabled="true"></DatabaseSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="目标库" prop="targetDatabase">
                            <DatabaseSelect :default-value="formModel.targetDatabase" :disabled="true"></DatabaseSelect>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="脚本" prop="scriptId">
                            <TaskScriptSelect :default-value="formModel.scriptId" :disabled="true"></TaskScriptSelect>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="查询分页数" prop="dataOffset">
                            <el-input-number v-model="formModel.dataOffset" :min="1" :max="100000" placeholder=""
                                clearable></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="目标表" prop="targetTable">
                            <el-input v-model="formModel.targetTable" show-word-limit maxlength="100" placeholder=""
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="操作类型" prop="targetOperateType">
                            <el-select v-model="formModel.targetOperateType" placeholder="">
                                <el-option v-for="item in DictInfo.targetOperateType" :key="item.key" :label="item.label"
                                    :value="item.key" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="执行前是否清数" prop="clearByBefore">
                            <el-select v-model="formModel.clearByBefore" placeholder="">
                                <el-option v-for="item in DictInfo.yesOrNo" :key="item.key" :label="item.label"
                                    :value="item.key" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formModel.remark" style="width: 600px;" :rows="2" type="textarea"
                                show-word-limit maxlength="500" placeholder="" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-divider v-if="formModel.taskType === 2" content-position="left">字段映射配置</el-divider>
            <FieldsList ref="fieldsRef" v-if="formModel.taskType === 2" :disabled="true" :data="formModel.fieldList">
            </FieldsList>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="() => closeDialog()" size="default">关 闭</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, defineComponent, inject, } from 'vue';
import { TaskInfoDetailModel } from '/@/interfaces/DataPipelineModel';
import useObjectUtil from '/@/utils/objectUtil';
import { DictInfo } from '/@/enums/dict-info-enum';
import DatabaseSelect from '/@/views/database-info/component/database-select.vue';
import TaskScriptSelect from '/@/views/task-script/component/task-script-select.vue';
import FieldsList from './fields-list.vue';

interface State {
    isShowDialog: boolean | true,
    submitLoading: boolean | false,
    formModel: TaskInfoDetailModel,
}
export default defineComponent({
    name: 'TaskInfoDetail',
    components: { DatabaseSelect, TaskScriptSelect, FieldsList },
    emits: ['onClose'],
    setup(props, context) {
        const state = reactive<State>({
            isShowDialog: true,
            submitLoading: false,
            formModel: {
                id: undefined,
                taskName: undefined,
                taskType: undefined,
                taskStatus: undefined,
                sourceDatabase: undefined,
                scriptId: undefined,
                dataOffset: undefined,
                targetDatabase: undefined,
                targetTable: undefined,
                targetOperateType: undefined,
                clearByBefore: undefined,
                remark: undefined,
                fieldList: [],
            },
        });
        const methodMap = reactive({
            // 关闭弹窗
            closeDialog: function () {
                state.isShowDialog = false;
                context.emit('onClose');
            }
        });
        // 表单赋值
        const detailData: TaskInfoDetailModel | undefined = inject<TaskInfoDetailModel>('detailData');
        if (undefined !== detailData) {
            useObjectUtil.copyProperties(detailData, state.formModel);
            state.formModel.fieldList = [...detailData.fieldList];
        } else {
            console.error("found undefined for detail");
        }
        return {
            DictInfo,
            ...toRefs(state),
            ...toRefs(methodMap),
        };
    },
});
</script>
